(function(){
    angular.module('app').controller("agbResourcesCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.platformList = [{name:'全部平台'},{id:'android',name:'Android'},{id:'ios',name:'iOS'}]
        $scope.resList = [{name:'全部资源类型'},{id:'cpc',name:'CPC'},{id:'cpm',name:'CPM'}]

        $scope.filter = {
            self: "0",
            executeState: "10, 20"
        }

        $scope.search = function(val) {
            $scope.filter.name = val;
            getList();
        }
        $scope.selectPlatform= function(item) {
            $scope.filter.platform = item.id;
            getList();
        }
        $scope.selectRes = function(item) {
            $scope.filter.chargeType = item.id;
            getList();
        }
        $scope.chooseDate = function(sTime,eTime){
            $scope.filter.startDate = sTime;
            $scope.filter.endDate = eTime;
            getList();
        }


        $scope.list = [];
		var masOption = {
			// options
			itemSelector: '.grid-item',
			columnWidth: 340
		}
		$('.grid').masonry(masOption).on('layoutComplete', function(){
			var n = Math.floor(($('#waterfallBox').width())/340);
			var w = n*340 + 20;
			$("#waterfallBox").width(w);
	   });
		$scope.$on('ngFinishRender', function(){
			$('.grid').masonry(masOption)
		});

        getList();

        function getList(current,pagesize) {
            var data = {
                'page': (current||1)+"",
                'size': (pagesize||20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter,
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/sku/show/list',
                data: data,
                load:true,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;

					$scope.list.forEach(function(sku){
                        var appInfo = {};
                        var temp = [];
                        sku.appDetail = sku.appDetail || [];
						sku.appDetail.forEach(function(space){
							var app = appInfo[space.appID];
							if(!app) {
								appInfo[space.appID] = space;
								temp.push({
									appName: space.appName,
									iconUrl: space.iconUrl,
									appType: space.appType
								})
							}
                        })
                        sku.appDetail = temp;
					})
                    $('.grid').masonry('destroy');
                    
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }
    })
})();